package qldc.pm.tienan.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.csdl.cd90.model.dto.CongDanDTO;
import qldc.pm.csdl.cd90.service.CongDanDTOLocalServiceUtil;
import qldc.pm.qtht.dmdc.model.PhuongXa;
import qldc.pm.qtht.dmdc.model.QuanHuyen;
import qldc.pm.qtht.dmdc.service.PhuongXaLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil;
import qldc.pm.qtht.dmdc.utils.DateUtils;
import qldc.pm.qtht.dmdc.utils.DiaChiUtils;
import qldc.pm.tienan.NoSuchYeuCauTrichLucException;
import qldc.pm.tienan.constants.TienAnConstants;
import qldc.pm.tienan.constants.TrangThaiTrichLucEnum;
import qldc.pm.tienan.dto.ThongKeTienAnDTO;
import qldc.pm.tienan.dto.YeuCauTrichLucDTO;
import qldc.pm.tienan.model.TienAn;
import qldc.pm.tienan.model.YeuCauTrichLuc;
import qldc.pm.tienan.model.impl.YeuCauTrichLucImpl;
import qldc.pm.tienan.service.base.YeuCauTrichLucLocalServiceBaseImpl;
import qldc.pm.tienan.service.persistence.TienAnFinderUtil;
import qldc.pm.tienan.util.Converters;
import qldc.pm.tienan.util.StringUtils;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the yeu cau trich luc local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link qldc.pm.tienan.service.YeuCauTrichLucLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tienan.service.base.YeuCauTrichLucLocalServiceBaseImpl
 * @see qldc.pm.tienan.service.YeuCauTrichLucLocalServiceUtil
 */
public class YeuCauTrichLucLocalServiceImpl extends YeuCauTrichLucLocalServiceBaseImpl {
	private static Log _log = LogFactoryUtil.getLog(YeuCauTrichLucLocalServiceImpl.class);

	

	@SuppressWarnings("null")
	public List<ThongKeTienAnDTO> thongKeYeuCauTrichLuc(long tinhId,
			long quanHuyenId, Date tuNgay, Date denNgay,
			String coQuanYeuCauTrichLuc, String nguoiTaoTrichLuc
			) {

		boolean isSelectQuanHuyen = quanHuyenId > 0;

		List<Object[]> objList ;
		try {
			if(isSelectQuanHuyen) {
				objList = TienAnFinderUtil.findByTKTS_Diaphuong_PhuongXa(tinhId, quanHuyenId, tuNgay, denNgay);
				return fetchThongKeTienAn(objList, tinhId, quanHuyenId, tuNgay, denNgay, coQuanYeuCauTrichLuc, nguoiTaoTrichLuc);
				
			} else {
				objList = TienAnFinderUtil.findByTKTS_Diaphuong_QuanHuyen(tinhId, tuNgay, denNgay);
				return fetchThongKeTienAn(objList, tinhId, quanHuyenId, tuNgay, denNgay, coQuanYeuCauTrichLuc, nguoiTaoTrichLuc);
			}
			
			
			

		} catch (SystemException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	private List<ThongKeTienAnDTO> fetchThongKeTienAn(List<Object[]> objList,
			long tinhId, long quanHuyenId, Date tuNgay, Date denNgay, String coQuanYeuCauTrichLuc,
			String nguoiTaoTrichLuc) {
		List<ThongKeTienAnDTO> dsThongKe = new ArrayList<ThongKeTienAnDTO>();
		long quanHuyen = 0, phuongXa = 0;

		try {

			for (int i = 0; i < objList.size(); i++) {
				Object[] obj = objList.get(i);
				ThongKeTienAnDTO thongKe = new ThongKeTienAnDTO();
				thongKe.setIndex(i);
				int hanhChinhId = Integer.valueOf(obj[0].toString());
				String tenHanhChinh;
				if (quanHuyenId <= 0) {
					tenHanhChinh = QuanHuyenLocalServiceUtil.findById(
							hanhChinhId).getTenQuanHuyen();
					quanHuyen = hanhChinhId;
					phuongXa = -1;
				} else {
					tenHanhChinh = PhuongXaLocalServiceUtil.findById(
							hanhChinhId).getTenPhuongXa();
					quanHuyen = quanHuyenId;
					phuongXa = hanhChinhId;
				}
				thongKe.setDonViHanhChinh(tenHanhChinh);
				thongKe.setHoKhau(Integer.valueOf(obj[1].toString()));
				thongKe.setNhanKhau(Integer.valueOf(obj[2].toString()));

				int hoCoTienAn = doiTuongTienAnFinder
						.countDSHoGiaDinhByVungDiaLy_ThoiGianToaAn(tinhId,
								quanHuyen, phuongXa, tuNgay, denNgay);
				int xoaAnTich = doiTuongTienAnFinder.countByDS_XoaAnTich(
						tinhId, quanHuyen, phuongXa, tuNgay, denNgay);
				int trichLuc = countByCQ_NguoiThucHienTaoTrichLuc_ThoiGian_VungDiaLy(coQuanYeuCauTrichLuc, nguoiTaoTrichLuc, tinhId, quanHuyen, phuongXa, tuNgay, denNgay);
				int tienAn = doiTuongTienAnFinder
						.countDsTienAnByVungDiaLy_ThoiGian(tinhId, quanHuyen,
								phuongXa, tuNgay, denNgay, 1);

				thongKe.setTienAn(tienAn);
				thongKe.setXoaAnTich(xoaAnTich);
				thongKe.setTongHoCoTienAn(hoCoTienAn);
				thongKe.setTrichLuc(trichLuc);
				dsThongKe.add(thongKe);
			}
		} catch (Exception e) {
				
		}

		return dsThongKe;
	}
	
	public void updateYeuCauTrichLucVoiSoDinhDanhCaNhan(long yeuCauTrichLucId, String soDinhDanhCaNhan) throws SystemException{
		YeuCauTrichLuc yctl = findByYeuCauTrichLucId(yeuCauTrichLucId);
		if (Validator.isNotNull(yctl)){
			yctl.setSoDinhDanhCaNhanDoiTuongTrichLuc(soDinhDanhCaNhan);
			CongDanDTO congDan = CongDanDTOLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
			if (Validator.isNotNull(congDan)){
				if (yctl.getDanTocId() <= 0)
					yctl.setDanTocId(congDan.getDanTocId());
				yctl.setGioiTinh(congDan.getGioiTinh());
				if (Validator.isNull(yctl.getHoTenVoHoacChong()))
					yctl.setHoTenVoHoacChong(congDan.getHoVaTenNguoiCoQuanHe());
				if (Validator.isNull(yctl.getHoVaTenCha()))
					yctl.setHoVaTenCha(congDan.getHoVaTenCha());
				if (Validator.isNull(yctl.getHoVaTenGoiKhac()))
					yctl.setHoVaTenGoiKhac(congDan.getTenGoiKhac());
				if (Validator.isNull(yctl.getHoVaTenMe()))
					yctl.setHoVaTenMe(congDan.getHoVaTenMe());
				if (Validator.isNull(yctl.getNoiCapCMNDDoiTuongTrichLuc()))
					yctl.setNoiCapCMNDDoiTuongTrichLuc(congDan.getNoiCapCMND());
				if (Validator.isNull(yctl.getSoCMNDDoiTuongTrichLuc()))
					yctl.setSoCMNDDoiTuongTrichLuc(congDan.getSoCMND());
				if (Validator.isNull(yctl.getNoiCapHoChieuDoiTuongTrichLuc()))
					yctl.setSoHoChieuDoiTuongTrichLuc(congDan.getSoHoChieu());
				if (Validator.isNull(yctl.getNoiSinh())){
					yctl.setNoiSinh( congDan.getNoiSinhDiaChi());
				}
				if (yctl.getNoiSinhPhuongXaId() <= 0){
					yctl.setNoiSinhPhuongXaId(congDan.getNoiSinhPhuongXaId());
				}
				if (yctl.getNoiSinhQuanHuyenId() <= 0){
					yctl.setNoiSinhQuanHuyenId(congDan.getNoiSinhQuanHuyenId());
				}
				if (yctl.getNoiSinhTinhId() <= 0){
					yctl.setNoiSinhTinhId(congDan.getNoiSinhQuanHuyenId());
				}
					
				if (Validator.isNull(yctl.getNgayCapCMNDDoiTuongTrichLuc()) && Validator.isNotNull(congDan.getNgayCapCMND())){
					//yctl.setNgayCapCMNDDoiTuongTrichLuc(ncongDan.getNgayCapCewMND());
				}
				if (Validator.isNull(yctl.getNgaySinhDoiTuongTrichLuc()) && Validator.isNotNull(congDan.getNgaySinh())){
					Date date = DateUtils.getDate(congDan.getNgaySinh(), TienAnConstants.DATE_PATTERN);
					yctl.setNgaySinhDoiTuongTrichLuc(date);
				}
				if (yctl.getNoiThuongTruDoiTuongTrichLucTinhId()  == 0)
					yctl.setNoiThuongTruDoiTuongTrichLucTinhId(congDan.getNoiThuongTruTinhId());
				if (yctl.getNoiThuongTruDoiTuongTrichLucQuanHuyenId() == 0)
					yctl.setNoiThuongTruDoiTuongTrichLucQuanHuyenId(congDan.getNoiThuongTruQuanHuyenId());
				if (yctl.getNoiThuongTruDoiTuongTrichLucPhuongXaId() == 0)
					yctl.setNoiThuongTruDoiTuongTrichLucPhuongXaId(congDan.getNoiThuongTruPhuongXaId());
				if (Validator.isNull(yctl.getNoiThuongTruDoiTuongTrichLucDiaChi()))
					yctl.setNoiThuongTruDoiTuongTrichLucDiaChi(congDan.getNoiThuongTruDiaChi());
				if (Validator.isNull(yctl.getQuocTich()))
					yctl.setQuocTich(congDan.getQuocTich());
				updateYeuCauTrichLuc(yctl);

			}
		}
	}
	public YeuCauTrichLuc addYeuCauTrichLuc(User user, String maYeuCauTrichLuc, 
			String hoVaTenNguoiYeuCauTrichLuc,
			long noiTamTruDoiTuongTrichLucTinhId,
			long noiTamTruDoiTuongTrichLucQuanHuyenId,
			long noiTamTruDoiTuongTrichLucPhuongXaId, String noiTamTruDoiTuongTrichLucDiaChi,
			String soCMNDDoiTuongTrichLuc,
			String soDinhDanhCaNhanDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, Date ngaySinhDoiTuongTrichLuc,
			long noiThuongTruDoiTuongTrichLucTinhId,
			long noiThuongTruDoiTuongTrichLucQuanHuyenId,
			long noiThuongTruDoiTuongTrichLucPhuongXaId,
			String noiThuongTruDoiTuongTrichLucDiaChi,
			String soSoHoKhauDoiTuongTrichLuc, String noiDungTrichLuc,
			Date ngayDeNghiTraTrichLuc, String chuThich, Date ngayCapSoCMND, String hoVaTenGoiKhac,
	    	String hoVaTenCha,
	    	String hoVaTenMe,
	    	String hoTenVoHoacChong, String kinhGui, String quocTich, long danTocId, int gioiTinh, Date ngayLapTrichLuc, String coQuanYeuCau, String noiCapCMND,String soHoChieu,String noiCapHoChieu,Date ngayCapHoChieu, String noiSinh, long noiSinhTinhId, long noiSinhQuanHuyenId, long noiSinhPhuongXaId
	    	)
	    	 throws SystemException {
    	long yeuCauTrichLucId = counterLocalService.increment(YeuCauTrichLuc.class.getName());
    	YeuCauTrichLuc yeuCauTrichLuc = createYeuCauTrichLuc(yeuCauTrichLucId);
		
    	// set audit fields
    	yeuCauTrichLuc.setUserId(user.getUserId());
    	yeuCauTrichLuc.setUserName(user.getScreenName());
    	yeuCauTrichLuc.setCreateDate(new Date());

    	yeuCauTrichLuc.setHoVaTenGoiKhac(hoVaTenGoiKhac);
    	yeuCauTrichLuc.setKinhGui(kinhGui);

    	yeuCauTrichLuc.setNgayCapCMNDDoiTuongTrichLuc(ngayCapSoCMND);
    	yeuCauTrichLuc.setNoiCapCMNDDoiTuongTrichLuc(noiCapCMND);
    	yeuCauTrichLuc.setSoHoChieuDoiTuongTrichLuc(soHoChieu);
    	yeuCauTrichLuc.setNoiCapHoChieuDoiTuongTrichLuc(noiCapHoChieu);
    	yeuCauTrichLuc.setNgayCapHoChieuDoiTuongTrichLuc(ngayCapHoChieu);
    	yeuCauTrichLuc.setNoiSinh(noiSinh);
    	yeuCauTrichLuc.setQuocTich(quocTich);
    	yeuCauTrichLuc.setGioiTinh(gioiTinh);
    	yeuCauTrichLuc.setDanTocId(danTocId);
    	yeuCauTrichLuc.setHoVaTenCha(hoVaTenCha);
    	yeuCauTrichLuc.setHoVaTenMe(hoVaTenMe);
    	yeuCauTrichLuc.setHoTenVoHoacChong(hoTenVoHoacChong);
    	yeuCauTrichLuc.setHoVaTenNguoiYeuCauTrichLuc(hoVaTenNguoiYeuCauTrichLuc);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucTinhId(noiTamTruDoiTuongTrichLucTinhId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucQuanHuyenId(noiTamTruDoiTuongTrichLucQuanHuyenId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucPhuongXaId(noiTamTruDoiTuongTrichLucPhuongXaId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucDiaChi(noiTamTruDoiTuongTrichLucDiaChi);
    	yeuCauTrichLuc.setSoCMNDDoiTuongTrichLuc(soCMNDDoiTuongTrichLuc);
    	yeuCauTrichLuc.setSoDinhDanhCaNhanDoiTuongTrichLuc(soDinhDanhCaNhanDoiTuongTrichLuc);
    	yeuCauTrichLuc.setHoVaTenDoiTuongTrichLuc(hoVaTenDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNgaySinhDoiTuongTrichLuc(ngaySinhDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucTinhId(noiThuongTruDoiTuongTrichLucTinhId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucQuanHuyenId(noiThuongTruDoiTuongTrichLucQuanHuyenId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucPhuongXaId(noiThuongTruDoiTuongTrichLucPhuongXaId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucDiaChi(noiThuongTruDoiTuongTrichLucDiaChi);
    	yeuCauTrichLuc.setSoSoHoKhauDoiTuongTrichLuc(soSoHoKhauDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNoiDungTrichLuc(noiDungTrichLuc);
    	yeuCauTrichLuc.setNgayDeNghiTraTrichLuc(ngayDeNghiTraTrichLuc);
    	yeuCauTrichLuc.setChuThich(chuThich);
    	yeuCauTrichLuc.setMaYeuCauTrichLuc(maYeuCauTrichLuc);
    	yeuCauTrichLuc.setNgayLapTrichLuc(ngayLapTrichLuc);
    	yeuCauTrichLuc.setCoQuanYeuCauTrichLuc(coQuanYeuCau);
    	yeuCauTrichLuc.setNoiSinhPhuongXaId(noiSinhPhuongXaId);
    	yeuCauTrichLuc.setNoiSinhQuanHuyenId(noiSinhQuanHuyenId);
    	yeuCauTrichLuc.setNoiSinhTinhId(noiSinhTinhId);
    	return addYeuCauTrichLuc(yeuCauTrichLuc);
    }

    public List<YeuCauTrichLuc> findAll() {
    	List<YeuCauTrichLuc> yeuCauTrichLucs = new ArrayList<YeuCauTrichLuc>();

    	try {
			yeuCauTrichLucs = yeuCauTrichLucPersistence.findAll();
		} catch (SystemException e) {
			_log.warn(e);
		}

    	return yeuCauTrichLucs;
    }

    public int countAll() {
    	int count = 0;

    	try {
			count = yeuCauTrichLucPersistence.countAll();
		} catch (SystemException e) {
			_log.warn(e);
		}

    	return count;
    }

    public YeuCauTrichLuc findByYeuCauTrichLucId(long yeuCauTrichLucId) {
    	YeuCauTrichLuc yeuCauTrichLuc = null;

    	try {
			yeuCauTrichLuc = yeuCauTrichLucPersistence.findByPrimaryKey(yeuCauTrichLucId);
		} catch (NoSuchYeuCauTrichLucException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

    	return yeuCauTrichLuc;
    }

	public YeuCauTrichLuc updateYeuCauTrichLuc(long yeuCauTrichLucId, User user, String maYeuCauTrichLuc, 
			String hoVaTenNguoiYeuCauTrichLuc,
			long noiTamTruDoiTuongTrichLucTinhId,
			long noiTamTruDoiTuongTrichLucQuanHuyenId,
			long noiTamTruDoiTuongTrichLucPhuongXaId, String noiTamTruDoiTuongTrichLucDiaChi,
			String soCMNDDoiTuongTrichLuc,
			String soDinhDanhCaNhanDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, Date ngaySinhDoiTuongTrichLuc,
			long noiThuongTruDoiTuongTrichLucTinhId,
			long noiThuongTruDoiTuongTrichLucQuanHuyenId,
			long noiThuongTruDoiTuongTrichLucPhuongXaId,
			String noiThuongTruDoiTuongTrichLucDiaChi,
			String soSoHoKhauDoiTuongTrichLuc, String noiDungTrichLuc,
			Date ngayDeNghiTraTrichLuc, String chuThich, Date ngayCapSoCMND, String hoVaTenGoiKhac,
	    	String hoVaTenCha,
	    	String hoVaTenMe,
	    	String hoTenVoHoacChong, String kinhGui, String quocTich, long danTocId, int gioiTinh, Date ngayLapTrichLuc, String coQuanYeuCau,String noiCapCMND,String soHoChieu,String noiCapHoChieu,Date ngayCapHoChieu, String noiSinh, long noiSinhTinhId, long noiSinhQuanHuyenId, long noiSinhPhuongXaId)
			throws PortalException, SystemException {
    	YeuCauTrichLuc yeuCauTrichLuc = getYeuCauTrichLuc(yeuCauTrichLucId);

    	// set audit fields
    	yeuCauTrichLuc.setUserId(user.getUserId());
    	yeuCauTrichLuc.setUserName(user.getScreenName());
    	yeuCauTrichLuc.setModifiedDate(new Date());

    	// other fields
    	yeuCauTrichLuc.setHoVaTenGoiKhac(hoVaTenGoiKhac);
    	yeuCauTrichLuc.setHoVaTenCha(hoVaTenCha);
    	yeuCauTrichLuc.setNoiSinh(noiSinh);
    	yeuCauTrichLuc.setHoVaTenMe(hoVaTenMe);
    	yeuCauTrichLuc.setKinhGui(kinhGui);
    	yeuCauTrichLuc.setNgayCapCMNDDoiTuongTrichLuc(ngayCapSoCMND);
    	yeuCauTrichLuc.setNoiCapCMNDDoiTuongTrichLuc(noiCapCMND);
    	yeuCauTrichLuc.setSoHoChieuDoiTuongTrichLuc(soHoChieu);
    	yeuCauTrichLuc.setNoiCapHoChieuDoiTuongTrichLuc(noiCapHoChieu);
    	yeuCauTrichLuc.setNgayCapHoChieuDoiTuongTrichLuc(ngayCapHoChieu);
    	yeuCauTrichLuc.setQuocTich(quocTich);
    	yeuCauTrichLuc.setDanTocId(danTocId);
    	yeuCauTrichLuc.setGioiTinh(gioiTinh);
    	yeuCauTrichLuc.setHoTenVoHoacChong(hoTenVoHoacChong);
    	yeuCauTrichLuc.setHoVaTenNguoiYeuCauTrichLuc(hoVaTenNguoiYeuCauTrichLuc);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucTinhId(noiTamTruDoiTuongTrichLucTinhId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucQuanHuyenId(noiTamTruDoiTuongTrichLucQuanHuyenId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucPhuongXaId(noiTamTruDoiTuongTrichLucPhuongXaId);
    	yeuCauTrichLuc.setNoiTamTruDoiTuongTrichLucDiaChi(noiTamTruDoiTuongTrichLucDiaChi);
    	yeuCauTrichLuc.setSoCMNDDoiTuongTrichLuc(soCMNDDoiTuongTrichLuc);
    	yeuCauTrichLuc.setSoDinhDanhCaNhanDoiTuongTrichLuc(soDinhDanhCaNhanDoiTuongTrichLuc);
    	yeuCauTrichLuc.setHoVaTenDoiTuongTrichLuc(hoVaTenDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNgaySinhDoiTuongTrichLuc(ngaySinhDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucTinhId(noiThuongTruDoiTuongTrichLucTinhId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucQuanHuyenId(noiThuongTruDoiTuongTrichLucQuanHuyenId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucPhuongXaId(noiThuongTruDoiTuongTrichLucPhuongXaId);
    	yeuCauTrichLuc.setNoiThuongTruDoiTuongTrichLucDiaChi(noiThuongTruDoiTuongTrichLucDiaChi);
    	yeuCauTrichLuc.setSoSoHoKhauDoiTuongTrichLuc(soSoHoKhauDoiTuongTrichLuc);
    	yeuCauTrichLuc.setNoiDungTrichLuc(noiDungTrichLuc);
    	yeuCauTrichLuc.setNgayDeNghiTraTrichLuc(ngayDeNghiTraTrichLuc);
    	yeuCauTrichLuc.setChuThich(chuThich);
    	yeuCauTrichLuc.setNgayLapTrichLuc(ngayLapTrichLuc);
    	yeuCauTrichLuc.setCoQuanYeuCauTrichLuc(coQuanYeuCau);

    	yeuCauTrichLuc.setNoiSinhPhuongXaId(noiSinhPhuongXaId);
    	yeuCauTrichLuc.setNoiSinhQuanHuyenId(noiSinhQuanHuyenId);
    	yeuCauTrichLuc.setNoiSinhTinhId(noiSinhTinhId);
    	
    	return updateYeuCauTrichLuc(yeuCauTrichLuc);
    }

	public List<YeuCauTrichLucDTO> findByCQ_NguoiThucHienTaoTrichLuc_ThoiGian_VungDiaLy(		
			String coQuanYeuCauTrichLuc,
			String nguoiTaoTrichLuc,
			long tinhIDCanBoTaoTrichLuc,
			long quanHuyenIDCanBoTaoTrichLuc, 
			long phuongXaIDCanBoTaoTrichLuc,
			Date tuNgayTaoYeuCau, Date denNgayTaoYeuCau, int start, int end, OrderByComparator orderByComparator){
		List<YeuCauTrichLucDTO> yeuCauTrichLucs = new ArrayList<YeuCauTrichLucDTO>();

		
		try {
			List<YeuCauTrichLuc>	list = yeuCauTrichLucFinder
						.findByCQ_NguoiThucHienTaoTrichLuc_ThoiGian_VungDiaLy(coQuanYeuCauTrichLuc, 
								nguoiTaoTrichLuc, tinhIDCanBoTaoTrichLuc, quanHuyenIDCanBoTaoTrichLuc,
								phuongXaIDCanBoTaoTrichLuc, tuNgayTaoYeuCau, denNgayTaoYeuCau,
								start, end, orderByComparator);
				//conver YeuCauTrichLuc to yeucautrichlucdto
				for (YeuCauTrichLuc yctl:list){
					YeuCauTrichLucDTO dto = Converters.convertFormEntityToDto_YeuCauTrichLuc(yctl);
					yeuCauTrichLucs.add(dto);
				}
			
		} catch (SystemException e) {
			_log.warn(e);
		}

		return yeuCauTrichLucs;
	}

	public int countByCQ_NguoiThucHienTaoTrichLuc_ThoiGian_VungDiaLy(
			String coQuanYeuCauTrichLuc,
			String nguoiTaoTrichLuc,
			long tinhIDCanBoTaoTrichLuc,
			long quanHuyenIDCanBoTaoTrichLuc, 
			long phuongXaIDCanBoTaoTrichLuc,
			Date tuNgayTaoYeuCau, Date denNgayTaoYeuCau) {
		int count = 0;

		try {
			
				count = yeuCauTrichLucFinder.countByCQ_NguoiThucHienTaoTrichLuc_ThoiGian_VungDiaLy(coQuanYeuCauTrichLuc, nguoiTaoTrichLuc, tinhIDCanBoTaoTrichLuc, quanHuyenIDCanBoTaoTrichLuc, phuongXaIDCanBoTaoTrichLuc, tuNgayTaoYeuCau, denNgayTaoYeuCau);

		} catch (SystemException e) {
			_log.warn(e);
		}

		return count;
	}
	
	public List<YeuCauTrichLuc> findBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_ThoiGian(int trangThaiTrichLuc,
			String soDinhDanhCaNhanDoiTuongTrichLuc,
			String hoVaTenNguoiYeuCauTrichLuc,String coQuanYeuCauTrichLuc,
			String soCMNDDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, String soSoHoKhauDoiTuongTrichLuc, Date tuNgayYeuCau, Date denNgayYeuCau, int start, int end, OrderByComparator orderByComparator) {
		List<YeuCauTrichLuc> yeuCauTrichLucs = new ArrayList<YeuCauTrichLuc>();

		
		try {
				yeuCauTrichLucs = yeuCauTrichLucFinder
						.findBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_ThoiGian(trangThaiTrichLuc, 
								soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenNguoiYeuCauTrichLuc,coQuanYeuCauTrichLuc, soCMNDDoiTuongTrichLuc,
								hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc, tuNgayYeuCau, denNgayYeuCau,
								start, end, orderByComparator);
			
		} catch (SystemException e) {
			_log.warn(e);
		}

		return yeuCauTrichLucs;
	}

	public int countBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_ThoiGian(int trangThaiTrichLuc,
			String soDinhDanhCaNhanDoiTuongTrichLuc, String hoVaTenNguoiYeuCauTrichLuc,String coQuanYeuCauTrichLuc,
			String soCMNDDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, String soSoHoKhauDoiTuongTrichLuc,Date tuNgayYeuCau, Date denNgayYeuCau) {
		int count = 0;

		try {
			
				count = yeuCauTrichLucFinder.countBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_ThoiGian(trangThaiTrichLuc,
						soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc,coQuanYeuCauTrichLuc, soCMNDDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc, tuNgayYeuCau, denNgayYeuCau);

		} catch (SystemException e) {
			_log.warn(e);
		}

		return count;
	}
	public List<YeuCauTrichLuc> findBySDDCN_NYCTL_CQ_CMND_DTTL_SHK(int trangThaiTrichLuc,
			String soDinhDanhCaNhanDoiTuongTrichLuc,
			String hoVaTenNguoiYeuCauTrichLuc,
			String soCMNDDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, String soSoHoKhauDoiTuongTrichLuc, int start, int end, OrderByComparator orderByComparator) {
		List<YeuCauTrichLuc> yeuCauTrichLucs = new ArrayList<YeuCauTrichLuc>();

		soDinhDanhCaNhanDoiTuongTrichLuc = StringUtils.getLike(soDinhDanhCaNhanDoiTuongTrichLuc);
		hoVaTenNguoiYeuCauTrichLuc = StringUtils.getLike(hoVaTenNguoiYeuCauTrichLuc);
		soCMNDDoiTuongTrichLuc = StringUtils.getLike(soCMNDDoiTuongTrichLuc);
		hoVaTenDoiTuongTrichLuc = StringUtils.getLike(hoVaTenDoiTuongTrichLuc);
		soSoHoKhauDoiTuongTrichLuc = StringUtils.getLike(soSoHoKhauDoiTuongTrichLuc);
		boolean daTrichLuc = false;
		if (trangThaiTrichLuc == TrangThaiTrichLucEnum.DA_TRICH_LUC.getValue()){
			daTrichLuc = true;
		}
		
		
		try {
			if (trangThaiTrichLuc == TrangThaiTrichLucEnum.TAT_CA.getValue()){
				yeuCauTrichLucs = yeuCauTrichLucPersistence
						.findBySDDCN_NYCTL_CQ_CMND_DTTL_SHK(soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenNguoiYeuCauTrichLuc, soCMNDDoiTuongTrichLuc,
								hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc,
								start, end, orderByComparator);
			}else {
				yeuCauTrichLucs = yeuCauTrichLucPersistence
						.findBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_TRANGTHAI(daTrichLuc, soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenNguoiYeuCauTrichLuc, soCMNDDoiTuongTrichLuc,
								hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc,
								start, end, orderByComparator);
			}
		} catch (SystemException e) {
			_log.warn(e);
		}

		return yeuCauTrichLucs;
	}

	public int countBySDDCN_NYCTL_CQ_CMND_DTTL_SHK(int trangThaiTrichLuc,String soDinhDanhCaNhanDoiTuongTrichLuc, String hoVaTenNguoiYeuCauTrichLuc,
			String soCMNDDoiTuongTrichLuc,
			String hoVaTenDoiTuongTrichLuc, String soSoHoKhauDoiTuongTrichLuc) {
		int count = 0;

		soDinhDanhCaNhanDoiTuongTrichLuc = StringUtils.getLike(soDinhDanhCaNhanDoiTuongTrichLuc);
		hoVaTenNguoiYeuCauTrichLuc = StringUtils.getLike(hoVaTenNguoiYeuCauTrichLuc);
		soCMNDDoiTuongTrichLuc = StringUtils.getLike(soCMNDDoiTuongTrichLuc);
		hoVaTenDoiTuongTrichLuc = StringUtils.getLike(hoVaTenDoiTuongTrichLuc);
		soSoHoKhauDoiTuongTrichLuc = StringUtils.getLike(soSoHoKhauDoiTuongTrichLuc);
		boolean daTrichLuc = false;
		if (trangThaiTrichLuc == TrangThaiTrichLucEnum.DA_TRICH_LUC.getValue()){
			daTrichLuc = true;
		}
		try {
			if (trangThaiTrichLuc == TrangThaiTrichLucEnum.TAT_CA.getValue()){				
				count = yeuCauTrichLucPersistence.countBySDDCN_NYCTL_CQ_CMND_DTTL_SHK(soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc, soCMNDDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc);
			} else {
				count = yeuCauTrichLucPersistence.countBySDDCN_NYCTL_CQ_CMND_DTTL_SHK_TRANGTHAI(daTrichLuc,soDinhDanhCaNhanDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc, soCMNDDoiTuongTrichLuc, hoVaTenDoiTuongTrichLuc, soSoHoKhauDoiTuongTrichLuc);

			}
		} catch (SystemException e) {
			_log.warn(e);
		}

		return count;
	}
}
